package com.formiaomiao.demain.service.impl;

import com.formiaomiao.demain.dao.LikeDao;
import com.formiaomiao.demain.model.LikeBean;
import com.formiaomiao.demain.model.Result;
import com.formiaomiao.demain.service.LikeService;
import com.formiaomiao.demain.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service(value = "LikeService")
public class LikeServiceImpl implements LikeService {

    @Autowired
    LikeDao likeDao;

    @Transactional(value = "demain")
    @Override
    public Result<String> updateLike(LikeBean likeBean) {
        String msg = "";
        if (likeBean.getLiketype() == 1) {
            msg = "点赞";
        } else {
            msg = "取消点赞";
        }
        if (likeDao.checkLike(likeBean.getLikeid(), likeBean.getLikerid()) == null) {
            if (likeDao.InsertLike(likeBean) == 0) {
                return ResultUtil.error(400, "点赞失败");
            } else {
                return ResultUtil.success("点赞成功");
            }
        } else {
            if (likeDao.updateLike(likeBean) == 0) {
                return ResultUtil.error(400, msg + "失败");
            } else {
                return ResultUtil.success(msg + "成功");
            }
        }
    }

}
